//
//  DialerView.m
//  LabanBrowser
//
//  Created by -Xh- on 3/13/13.
//  Copyright (c) 2013 VNG. All rights reserved.
//

#import "DialerView.h"
#import "SpeedDialAccess.h"
#import "StaticVariables.h"
@implementation DialerView
@synthesize speedDialList;
double kCellWidth=60;
double kCellHeight=75;
double kItemWidth=60;
double kItemHeight=75;
double kItemInset=10;
- (id)initWithFrame:(CGRect)frame

{
    UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc] init];
    //[flowLayout setScrollDirection:UICollectionViewScrollDirectionHorizontal];
    [flowLayout setItemSize:CGSizeMake(kCellWidth, kCellHeight)];
    [flowLayout setMinimumInteritemSpacing:10];
    [flowLayout setMinimumLineSpacing:10];
    [flowLayout setSectionInset:UIEdgeInsetsMake(kItemInset, kItemInset, kItemInset, kItemInset)];
    [flowLayout setItemSize:CGSizeMake(kItemWidth, kItemHeight)];
    self = [super initWithFrame:frame collectionViewLayout:flowLayout];
    if (self) {
        // Initialization code
        self.backgroundColor=[UIColor whiteColor];
        [self setBounces:NO];
        [self setScrollsToTop:NO];
        [self registerClass:[DialerItem class] forCellWithReuseIdentifier:@"DialerReuseID"];
        self.autoresizingMask = UIViewAutoresizingFlexibleBottomMargin;
//        self.layer.borderColor = [[UIColor redColor]CGColor];
//        self.layer.borderWidth = 2;
        

    }
    return self;
}

-(void)populateSpeedDialFromDB{

    speedDialList=[[[StaticVariables shareVariables]speedDialAccess] getAllRecords];
    [self performSelectorOnMainThread:@selector(reloadData) withObject:nil waitUntilDone:NO];
}

-(void)insertSpeedDial:(SpeedDialModel *)_speedDial{
    
    //[self performBatchUpdates:^{
        NSIndexPath* lastIndexPath=[NSIndexPath indexPathForRow:speedDialList.count-1 inSection:0];
        NSArray* lastIndexPathArray=[NSArray arrayWithObject:lastIndexPath];
        [self insertItemsAtIndexPaths:lastIndexPathArray];
        [speedDialList addObject:_speedDial];
        [self reloadItemsAtIndexPaths:lastIndexPathArray];
    //} completion:^(BOOL finished) {
        
    //}];
}

-(SpeedDialModel *)speedDialByURL:(NSString *)_url{
    for(int i=0;i<speedDialList.count;i++){
        SpeedDialModel* iModel=[speedDialList objectAtIndex:i];
        if([iModel.url isEqualToString:_url]){
            return iModel;
        }
    }
    return nil;
}

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect
{
    // Drawing code
}
*/

@end
